package com.example.theme_hospital;

import java.awt.*;

public class Doctor {
    public int x, y;
    private int speed = 1;
    private Patient currentPatient = null;
    private int diagnosisProgress = 0;
    private int treatmentProgress = 0;
    
    public Doctor(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void move(int dx, int dy, int[][] map, int mapWidth, int mapHeight) {
        // 计算新位置
        int newX = x + dx * speed;
        int newY = y + dy * speed;
        
        // 检查边界和碰撞
        if (newX >= 0 && newX < mapWidth && newY >= 0 && newY < mapHeight) {
            // 检查是否可以移动到新位置（不是墙壁）
            if (map[newY][newX] != HospitalGamePanel.WALL) {
                x = newX;
                y = newY;
            }
        }
    }
    
    public void update() {
        // 更新医生状态
        // 如果正在诊断或治疗病人，增加进度
        if (currentPatient != null) {
            Diagnosis diagnosis = currentPatient.getDiagnosis();
            
            // 如果还未诊断，进行诊断
            if (!diagnosis.isDiagnosed()) {
                diagnosisProgress++;
                if (diagnosisProgress >= 100) { // 诊断需要100帧
                    diagnosis.diagnose();
                    diagnosisProgress = 0;
                }
            } 
            // 如果已诊断但未治疗完成，进行治疗
            else if (!diagnosis.isTreated()) {
                treatmentProgress++;
                if (treatmentProgress >= 5) { // 每5帧治疗一次
                    diagnosis.treat();
                    treatmentProgress = 0;
                }
            }
        }
    }
    
    public void render(Graphics2D g, int isoX, int isoY) {
        // 绘制医生为红色菱形
        g.setColor(Color.RED);
        
        // 绘制菱形
        int[] xPoints = {isoX, isoX + HospitalGamePanel.TILE_WIDTH/4, isoX, isoX - HospitalGamePanel.TILE_WIDTH/4};
        int[] yPoints = {isoY - HospitalGamePanel.TILE_HEIGHT/4, isoY, isoY + HospitalGamePanel.TILE_HEIGHT/4, isoY};
        g.fillPolygon(xPoints, yPoints, 4);
        
        // 绘制网格线
        g.setColor(Color.BLACK);
        g.drawPolygon(xPoints, yPoints, 4);
        
        // 如果正在处理病人，显示进度条
        if (currentPatient != null) {
            Diagnosis diagnosis = currentPatient.getDiagnosis();
            if (!diagnosis.isDiagnosed()) {
                // 显示诊断进度条（使用与地面反差较大的颜色：白色边框，蓝色填充）
                g.setColor(Color.BLUE);
                int barWidth = 20;
                int barHeight = 3;
                int barX = isoX - barWidth/2;
                int barY = isoY - HospitalGamePanel.TILE_HEIGHT/4 - 10;
                g.fillRect(barX, barY, (int)(barWidth * (diagnosisProgress / 100.0)), barHeight);
                
                g.setColor(Color.WHITE);
                g.drawRect(barX, barY, barWidth, barHeight);
            } else if (!diagnosis.isTreated()) {
                // 显示治疗进度条（使用与地面反差较大的颜色：白色边框，绿色填充）
                g.setColor(Color.GREEN);
                int barWidth = 20;
                int barHeight = 3;
                int barX = isoX - barWidth/2;
                int barY = isoY - HospitalGamePanel.TILE_HEIGHT/4 - 10;
                int progressWidth = (int)(barWidth * (1 - (diagnosis.getTreatmentTime() / (double)(diagnosis.getSeverity() * 100))));
                g.fillRect(barX, barY, progressWidth, barHeight);
                
                g.setColor(Color.WHITE);
                g.drawRect(barX, barY, barWidth, barHeight);
            }
        }
    }
    
    // 设置当前处理的病人
    public void setCurrentPatient(Patient patient) {
        this.currentPatient = patient;
        this.diagnosisProgress = 0;
        this.treatmentProgress = 0;
    }
    
    // Getters
    public Patient getCurrentPatient() {
        return currentPatient;
    }
}